package src

import (
	"time"

	"gitee.com/haifengat/goctp"
	"github.com/sirupsen/logrus"
)

func Run() {
	startTrade()
	tick := time.NewTicker(1 * time.Minute)
	for range tick.C {
		hhmm := time.Now().Local().Format("15:04")
		if t.IsLogin { // 收盘后退出
			isContinue := false
			t.InstrumentStatuss.Range(func(key, value interface{}) bool {
				if value.(*goctp.InstrumentStatus).InstrumentStatus == goctp.InstrumentStatusContinous {
					isContinue = true
					return false
				}
				return true
			})
			if !isContinue {
				hour := time.Now().Local().Hour()
				if hour <= 3 || hour == 15 { // 夜盘结束&当日收盘
					logrus.Info("release at ", time.Now().Local())
					releaseTrade()
					releaseQuote()
				}
			}
		} else { // 定时连接
			if time.Now().Local().Minute()%15 == 0 { // 15 分钟显示一次
				logrus.Info(hhmm, " 接口关闭")
			}
			if hhmm == "08:40" || hhmm == "20:40" {
				// 周末过滤,[周末连接后不会返回OnConnected,而是一直输出 4096 日志.]
				if time.Now().Local().Weekday() == time.Sunday || time.Now().Local().Weekday() == time.Saturday {
					continue
				}
				logrus.Info("relogin at ", hhmm)
				startTrade()
			}
		}
	}
}
